<?php 

Class menuModel extends appModel
{
	/**
	 * 菜单栏目排序
	 * @param $val
	 * @param $id
	 * @return multitype:number string
	 */
	public function ordering($val,$id)
	{
		$result = array('error' => 0, 'content' => '排序成功');
		$sql = "UPDATE ".$this->table('category')." SET ordering='$val' WHERE id='".$id."'";
		$res = $this->query($sql);
		if(!res)
		{
			$result['error'] = 1;
			$result['content'] = '排序失败';
		}
		return $result;
	}

	/**
	 * 获取菜单栏目详细信息
	 * @param $id
	 * @return 
	 */
   	public function get_menu_info($id)
   	{
   		$sql = " SELECT * FROM ".$this->table('category')." WHERE id=".$id;
   		$info = $this->getRow($sql);
   		$arr3 = hy_dig_filter($info['name'], 3);
		$info['en_name'] = $arr3[1];
		$info['name'] = $arr3[0];
   		$sql = " SELECT name FROM ".$this->table('category')." WHERE id=".$info['parent_id'];
   		$info['parent_name'] = hy_dig_filter($this->getOne($sql), 1);
   		$temp = explode(',', $info['nav_pos']);
   		$info['nav_pos'] = array();
   		foreach($temp as $k => $v)
   		{
   			$info['nav_pos'][$v] = 1;
   		}
   		switch($info['type'])
   		{
   			case 1:
   				$info['type_name']='文章模块';
   				break;
   			case 2:
   				$info['type_name']='相册模块';
   				break;
   			case 3:
   				$info['type_name']='图文模块';
   				break;
   			case 4:
   				$info['type_name']='产品模块';
   				break;
   		}
   		return $info;
   	}
   
   	
   	public function del_menu($id,$is_all=false)
   	{
   		$result=array('error'=>0, 'content'=>'删除成功');
   		if($id)
   		{
   			$sql = "SELECT count(*) FROM ".$this->table('category')." WHERE parent_id=".$id;
   			$child_menu = $this->getOne($sql);
   		
   			if($child_menu > 0)
   			{
   				$result['error'] = 1;
   				$result['content'] = '栏目存在子级别栏目';
   				return $result;
   			}
   			if($is_all == true)
   			{
   				$sql = "SELECT type FROM ".$this->table('category')." WHERE id=".$id;
   				$type = $this->getOne($sql);

   				if(in_array($type, array(1,2,3,4)))
   				{
   					switch($type)
   					{
   						case 1:
   							$table = $this->table('article');
   							break;
   						case 2:
   							$table = $this->table('albums');
   							break;
   						case 3:
   							$table = $this->table('single_pages');
   							break;
   						case 4:
   							$table = $this->table('product');
   							break;
   					}
   					$sql = "SELECT count(*) FROM ".$table." WHERE cat_id='".$id."'";
   			
   					$content_num = $this->getOne($sql);
   					if($content_num > 0 && $type != 3)
   					{
   						$result['error'] = 1;
   						$result['content'] = '栏目下面存在内容';
   						return $result;
   					}
   					else
   					{
   						$sql = "DELETE FROM ".$this->table('category')." WHERE  id='".$id."'";
   			
   						$res = $this->query($sql);
   						if($type == 3)
   						{
   							$sql = "DELETE FROM ".$this->table('single_pages')." WHERE  cat_id='".$id."'";
   							$res = $this->query($sql);
   						}
   						if(!$res)
   						{
   							$result['error'] = 1;
   							$result['content'] = '删除失败';
   							return $result;
   						}
   					}
   				}
   			}
			else
   			{
   				$sql = "DELETE FROM ".$this->table('category')." WHERE  id='".$id."'";
   			
   				$res = $this->query($sql);
   			 
   				if(!$res)
   				{
   					$result['error'] = 1;
   					$result['content'] = '删除失败';
   					return $result;
   				}
   			}
   		}
   		return $result;
   	}
   
    /**
     * 不分页获取菜单列表
     *
     * @access      public
     * @param       int      page     当前页
     * @param       int      pagesize     每页显示数量
     * @param       int     show_extend_link     是否显示外链
     * @return      array
     */
    public function get_cat_list($sort=array())
    {
    	$where = ' WHERE 1=1 ';
    	$sql = 'SELECT name,id,parent_id,ordering,type FROM '.$this->table('category').$where;
    	$cat = $this->getAll($sql);
    	foreach($cat as $k => $v)
    	{
    		$cat[$k]['name'] = hy_dig_filter($v['name'], 1);
    		switch($v['type'])
    		{
    			case 1: 
    				$cat[$k]['cat_type']='文章模块';
    				break;
    			case 2: 
    				$cat[$k]['cat_type']='相册模块';
    				break;
    			case 3: 
    				$cat[$k]['cat_type']='单页模块';
    				break;
    			case 4: 
    				$cat[$k]['cat_type']='产品模块';
    				break;
    		}
    	}
    	$res = self::sortdata($cat, 0, '----|');
    	if(!empty($sort))
    	{
    		foreach($res as $k => $v)
    		{
    			if($sort['type'])//筛选类型
    			{
    			   	if(is_array($sort['type']))
    			  	{
    				   	if(!in_array($v['type'], $sort['type']))
    				  	{
    						unset($res[$k]);
    				  	}
    			  	}
    			   	else
    			  	{
    				 	if($v['type'] != $sort['type'])
    				 	{
    						unset($res[$k]);
    				 	}
    			  	}
    		   	}
    		}
    	}
    	return $res;
    }
    
    /**
     * @param
     * @return array
     * @brief 无限极分类递归函数
     */
    public static function sortdata($catArray, $id = 0 , $prefix = '')
    {
    	static $formatCat = array();
    	static $floor     = 0;
    
    	foreach($catArray as $key => $val)
    	{
    		if($val['parent_id'] == $id)
    		{
    			//处理子分类缩进效果
    			$return='';
    			for($i=0;$i<$floor;$i++)
    			{
    			$return .= $prefix;
    			}
    			$str         = $return;
    			
    			$val['name'] = $str.$val['name'];
    
    			$val['floor'] = $floor;
    			$formatCat[]  = $val;
    
    			unset($catArray[$key]);
    
    			$floor++;
    			self::sortdata($catArray, $val['id'] ,$prefix);
    			$floor--;
    		}
    		
    	}
    	return $formatCat;
    }
    
    /**
     * 插入菜单数据
     * @param $info
     * @return boolean
     */
    public function insert_menu($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "INSERT INTO ".$this->table('category')." (".$data['key'].") VALUES(".$data['val'].")";
			$this->query($sql);
			if($this->insert_id())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return false;
	}
	
	/**
	 * 更新数据库数据
	 * @param $info
	 * @param $id
	 */
	public function update_menu($info, $id)
	{
		if($id)
		{
			$data = yf_set_update($info);
			$sql="UPDATE ".$this->table('category')." SET ".$data." WHERE id='".$id."'";
			$this->query($sql);
			return true;
		}
	}
	
	/**
	 * 确认code是否已存在
	 * @param $code
	 * @param $id
	 * @return boolean
	 */
	public function check_code($code, $id = 0)
	{
		if($id > 0)
		{
			$sql = "SELECT code FROM ".$this->table('category')." WHERE id='".$id."'";
			$oldcode = $this->getOne($sql);
			$code_exist = 0;
			if($oldcode != $code)
			{
				$sql = "SELECT count(*) FROM ".$this->table('category')." WHERE code='$code'";
				$code_exist = $this->getOne($sql);			
			}
				
			if($code_exist>0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			$sql="SELECT count(*) FROM ".$this->table('category')." WHERE code='$code'";
			if($this->getOne($sql) > 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}